package com.qiche.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.qiche.mapper.GiftPackageMapper;
import com.qiche.service.GiftPackageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.mapper.MerchantMapper;
import com.qiche.mapper.UserRoleMapper;
import com.qiche.model.GiftPackage;
import com.qiche.model.User;

import tk.mybatis.mapper.entity.Example;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("giftPackageService")
public class GiftPackageServiceImpl extends BaseService<GiftPackage> implements GiftPackageService {
	@Resource
	private GiftPackageMapper packMapper;

	@Override
	public PageInfo<GiftPackage> selectByPage(GiftPackage pack, int page, int limit) {
		Example example = new Example(GiftPackage.class);
		Example.Criteria criteria = example.createCriteria();
		if (pack.getMerchantId() != null && pack.getMerchantId() != 0) {
			criteria.andEqualTo("merchantId",pack.getMerchantId());
		} 
		if (StringUtils.isNotBlank(pack.getCode())) {
			criteria.andLike("policyCode", "%" + pack.getCode()+"%" );
		}
		if (StringUtils.isNotBlank(pack.getPhone())) {
			criteria.andLike("phone", "%" + pack.getPhone()+"%");
		}
		if (StringUtils.isNotBlank(pack.getPlateNumber())) {
			criteria.andLike("plateNumber", "%" + pack.getPlateNumber()+"%");
		}
		// 分页查询
		PageHelper.startPage(page, limit);
		List<GiftPackage> packList = selectByExample(example);
		return new PageInfo<>(packList);
	}

	@Override
	public GiftPackage selectById(Long packId) {
		Example example = new Example(GiftPackage.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", packId);
		List<GiftPackage> packList = selectByExample(example);
		if (packList.size() > 0) {
			return packList.get(0);
		}
		return null;
	}

	@Override
	public GiftPackage selectByMemberAndPolicy(Long memberId, Long policyId) {
		Example example = new Example(GiftPackage.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("ownerId", memberId);
		criteria.andEqualTo("policyId", policyId);
		List<GiftPackage> packList = selectByExample(example);
		if (packList.size() > 0) {
			return packList.get(0);
		}
		return null;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public void delGiftPackage(Long packId) {
		mapper.deleteByPrimaryKey(packId);
	}


}
